import $HTTP from '@/common/lib/request.js';
import tools from '@/common/js/tools.js';
import user from "@/store/modules/user.js"
export default {
	state: {
		// 购物车存储的商品列表
		list: [],
		// 选中列表（存放选中的goods_id）
		selectedList: [],
		// 选中列表（存放选中的obj）
		selectedListObj: [],
		// 选择列表 (存放选中的rec_id)
		selectedRecId: [],
		// 为避免多次触发导致数据未生成
		lodingStatus:false,
	},
	getters: {
		// 判断是否全选
		checkedAll: (state) => {
			return state.list.length === state.selectedList.length
		},
		// 合计金额 不包含礼包
		totalPrice: (state) => {
			var total = 0;
			state.list.forEach(v => {
				console.log('state.selectedList',state.selectedList,'state.selectedRecId',state.selectedRecId)
				if ((state.selectedList.indexOf(v.goods_id) > -1) && (state.selectedRecId.indexOf(v.rec_id) > -1)) {
					// 不可简写
					total = tools.accAdd(total, tools.accMul(+v.vip_price, +v.goods_number))
					console.log('total', total)
				}
			})
			state.discountPrice = total;
			state.lodingStatus = false;
			
			console.log('tottal', total, typeof total)
			return +total;
		},
		// 合计金额 包含礼包
		libaoTotalPrice: (state) => {
			var total = state.discountPrice,
				num = 0;
			state.list.forEach(v => {
				if (state.selectedList.indexOf(v.goods_id) > -1) {
					// 先获取使用的礼包个数
					if (v.is_suit == 1) {
						num += +v.goods_number;
					}
				}
			})
			if (num != 0) {
				/**
				 * 套餐总价是198  使用礼包是58原价
				 * 获取用户当前的礼包数 
				 * 1. 礼包数大于商品数 = 商品数
				 * 2. 礼包数小于商品数 = 礼包数
				 * */
				var libao = +user.state.userInfo.club_card,
					reduceNum = 0,
					priseNum = 0;

				if (libao >= num) {
					reduceNum = num;
				} else {
					reduceNum = libao;
				}
				console.log('reduceNum',reduceNum)
				priseNum = tools.accMul(reduceNum, 140);
				console.log('this.$store.state', total, priseNum)
				return tools.accSub(parseFloat(total), priseNum);
			}
			console.log('tottal', total, typeof total)
			return parseFloat(total);
		},
		// 禁用全选
		disableSelectAll: (state) => {
			return state.list.length === 0
		},
		// 根据id 去list 里面的商品信息
		getStateList: (state) => {
			return state.list.filter(v => {
				return (state.selectedList.indexOf(v.goods_id) != -1) && (state.selectedRecId.indexOf(v.rec_id) != -1);
			})
		},
		
	
	},
	mutations: {
		// 初始化list
		initCartList(state, list) {
			state.list = list
		},
		// 添加购物车
		addCart(state, goods) {
			// 判断该商品是否已经在vux 里面已经存在
			let status = state.list.some(v => {
				return goods.goods_id === v.goods_id
			})
			console.log('status‘', status)
			if (!status) {
				state.list.unshift(goods)
			}
		},
		// 选中/取消选中某个商品
		selectItem(state, index) {
			state.lodingStatus = true;
			let id = state.list[index].goods_id,
				recId = state.list[index].rec_id;
			let i = state.selectedRecId.indexOf(recId)
			// 之前是选中，取消选中 
			if (i > -1) {
				// 取消当前商品选中状态
				state.list[index].checked = false;
				// 移除productid 
				state.selectedRecId.splice(state.selectedRecId.indexOf(recId), 1);
				// 移除选中列表中的当前商品
				return state.selectedList.splice(i, 1)
			}
			// 选中
			state.list[index].checked = true
			state.selectedList.push(id)
			state.selectedRecId.push(recId)
		},
		// 全选
		selectAll(state) {
			state.selectedList = state.list.map(v => {
				// 设置选中状态
				v.checked = true;
				state.selectedRecId.push(v.rec_id);
				return v.goods_id
			})
		},
		// 根据selectedList 里面的id 获取list 指定商品
		filterList(state) {
			var arr = [];
			state.list.forEach(v => {
				if (state.selectedList.indexOf(v.goods_id) > -1) {
					arr.push(v);
				}
			})
			state.selectedListObj = arr;
		},
		// 取消全选
		unSelectAll(state) {
			state.list.forEach(v => {
				// 设置选中状态
				v.checked = false
			})
			state.selectedList = [];
			state.selectedRecId = [];
		},
		// 删除选中
		delGoods(state) {
			state.list = state.list.filter(v => {
				return state.selectedList.indexOf(v.goods_id) === -1
			})
			// 清空选中列表
			state.selectedList = [];
		},
		// 库存的增加/减少
		numberChange(state, arr) {
			state.list[arr[1]].goods_number = arr[0];
			console.log('numberChange', arr);
		},

	},
	actions: {
		// 更新购物车列表
		updateCartList({
			state,
			commit
		}) {
			return new Promise((res, rej) => {
				$HTTP.post('/api/api.php', {
					act: 'get_cart'
				}, {
					token: true
				}).then(data => {
					// 取消选中状态
					if(data.error) res(data);
					commit('unSelectAll')
					// // 赋值
					commit('initCartList', data.lists.list)
					res(data)
				}).catch(err => {
					rej(err)
				})
			})
		},
		// 全选与反选
		doSelectAll({
			commit,
			getters
		}) {
			console.log(commit)
			getters.checkedAll ? commit('unSelectAll') : commit('selectAll')
		},
		// 多个删除
		doDelGoods({
			commit,
			state
		}) {
			if (state.selectedList.length === 0) {
				return uni.showToast({
					title: '请选择要删除的商品',
					icon: 'none'
				});
			}
			uni.showModal({
				content: '是否删除选中',
				success: (res) => {
					if (res.confirm) {
						commit('filterList');
						let options = {
							act: 'edit_cart',
							type: 'del',
							goods: state.selectedListObj
						}
						$HTTP.postStatus('/api/api.php', options, {
							token: true
						}).then(res => {

							if (res.result === 1002) {
								// 根据下标删除商品
								commit('delGoods');
								uni.showToast({
									title: data.msg,
									icon: 'none'
								});
							}
						})
					}
				}
			});
		}
	}

}
